import time
import unittest
import requests
from api.api_register_login import ApiRegisterLogin
from parameterized import parameterized
from utils import read_json
from utils import GetLog
log = GetLog.get_logger()


class TestRegisterLogin(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        log.info('正在执行接口初始化--{}'.format(self.session))
        self.reg = ApiRegisterLogin(self.session)

    def tearDown(self) -> None:
        self.session.close()
        log.info('正在关闭接口--')

    @parameterized.expand(read_json('register_login.json', 'img_code'))
    def test01_img_code(self, random, expect):
        try:
            r = self.reg.api_img_code(random)
            log.info('执行接口结果为：{}'.format(r.status_code))
            self.assertEqual(expect, r.status_code)
            log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise

    @parameterized.expand(read_json('register_login.json', 'phone_code'))
    def test02_phone_code(self, phone, imgVerifyCode, expect):
        try:
            self.reg.api_img_code(123)
            r = self.reg.api_phone_code(phone, imgVerifyCode)
            log.info('执行接口结果为：{}'.format(r.json()))
            self.assertIn(expect, r.text)
            log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise

    @parameterized.expand(read_json('register_login.json', 'register'))
    def test03_register(self, phone, password, imgVerifyCode, phone_code, expect):
        try:
            self.reg.api_img_code(123)
            self.reg.api_phone_code(phone='17666666604', imgVerifyCode='8888')
            r = self.reg.api_register(phone, password, imgVerifyCode, phone_code)
            log.info('执行接口结果为：{}'.format(r.json()))
            self.assertIn(expect, r.text)
            log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise

    @parameterized.expand(read_json('register_login.json', 'login'))
    def test04_login(self, keywords, password, expect):
        try:
            n = 1
            r = None
            if 'error' in password:
                while n <= 3:
                    r = self.reg.api_login(keywords=keywords, password=password)
                    n += 1
                self.assertIn('锁定', r.text)
                log.info('执行接口结果为：{}'.format(r.json()))
                time.sleep(1)
                r = self.reg.api_login(keywords=keywords, password='test123')
                self.assertIn(expect, r.text)
                log.info('执行断言通过')
            else:
                r = self.reg.api_login(keywords=keywords, password=password)
                log.info('执行接口结果为：{}'.format(r.json()))
                self.assertIn(expect, r.text)
                log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise

    @parameterized.expand(read_json('register_login.json', 'login_status'))
    def test05_login_status(self, status, expect):
        try:
            if '已登录' in status:
                self.reg.api_login(keywords='17666666660', password='test123')
                r = self.reg.api_login_status()
                log.info('执行接口结果为：{}'.format(r.json()))
                self.assertIn(expect, r.text)
                log.info('执行断言通过')
            else:
                r = self.reg.api_login_status()
                log.info('执行接口结果为：{}'.format(r.json()))
                self.assertIn(expect, r.text)
                log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise
